package com.lijin.study.java.other;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;


public class FileTest {

	public static void main(String[] args) throws Exception {
		System.out.println("------------------DEFAULT------------------");
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("Result"), "GBK"));
		String nm = "中国民生北京分";
		String fnm = fullFillStr(nm, 30, " ", "right");
		System.out.println("【" + fnm + "】");
		System.out.println("【" + fnm.length() + "】");
		System.out.println("【" + fnm.getBytes().length + "】");
		System.out.println("【" + fnm.getBytes("GBK").length + "】");
		bw.write(fnm);
		bw.flush();
		bw.close();
		System.out.println("------------------GBK------------------");
		bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("Result2"), "GBK"));
		fnm = fullFillStr2(nm, 30, " ", "right");
		System.out.println("【" + fnm + "】");
		System.out.println("【" + fnm.length() + "】");
		System.out.println("【" + fnm.getBytes().length + "】");
		System.out.println("【" + fnm.getBytes("GBK").length + "】");
		bw.write(fnm);
		bw.flush();
		bw.close();
	}
	
	private static String fullFillStr(String str, int totalLength, String ch, String pos) throws Exception {
		if (str == null) {
			str = "";
		} else {
			str = str.trim();
		}
		
		StringBuilder result = new StringBuilder();
		byte[] b = str.getBytes("GBK");
		System.out.println(b.length);
		int l = totalLength - b.length;
		System.out.println("l--->" + l);
		if (l < 0) {
			byte[] b2 = new byte[30];
			for (int i = 0; i < 30; i ++) {
				b2[i] = b[i];
			}
			// 如果超过指定的长度，则把原字符截取一定的位数返回
			result.append(new String(b2));
		} else {
			// 如果标志位为左，则左补指定的字符
			if (pos.equals("left")) {
				for (int i = 0; i < l; i ++) {
					result.append(ch);
				}
			}
			
			result.append(new String(b));
			
			// 如果标志位为右，则右补指定的字符			
			if (pos.equals("right")) {
				for (int i = 0; i < l; i ++) {
					result.append(ch);
				}
			}
		}
		
		return result.toString();
	}
	
	private static String fullFillStr2(String str, int totalLength, String ch, String pos) throws Exception {
		if (str == null) {
			str = "";
		} else {
			str = str.trim();
		}
		
		StringBuilder result = new StringBuilder();
		byte[] b = str.getBytes("GBK");
		System.out.println(b.length);
		int l = totalLength - b.length;
		System.out.println("l--->" + l);
		if (l < 0) {
			byte[] b2 = new byte[30];
			for (int i = 0; i < 30; i ++) {
				b2[i] = b[i];
			}
			// 如果超过指定的长度，则把原字符截取一定的位数返回
			result.append(new String(b2));
		} else {
			// 如果标志位为左，则左补指定的字符
			if (pos.equals("left")) {
				for (int i = 0; i < l; i ++) {
					result.append(ch);
				}
			}
			
			result.append(new String(b, "GBK"));
			
			// 如果标志位为右，则右补指定的字符			
			if (pos.equals("right")) {
				for (int i = 0; i < l; i ++) {
					result.append(ch);
				}
			}
		}
		
		return result.toString();
	}
}
